<template>
  <div class="login" id="particles-js">

    <!--    //登录框-->
    <div class="loginPart">
      <h3>用户登录</h3>
      <div class="formpart">
        <el-form
            ref="ruleForm"
            :model="admin"
            :rules="rules"
            label-width="100px"
            style="transform:translate(-30px)">

          <el-form-item label="账号：" prop="name" class="item">
            <el-input v-model="admin.name" size="small" placeholder="Name..." maxlength="8" clearable/>
          </el-form-item>
          <el-form-item label="密码：" prop="password" class="item">
            <el-input v-model="admin.password" size="small" type="password" placeholder="Password..." maxlength="10"
                      show-password
                      clearable/>
          </el-form-item>
        </el-form>

        <div style="text-align: center;margin-bottom: 10px">
          <el-button type="info" size="mini" @click="submitLogin" plain>登录</el-button>
        </div>
      </div>
    </div>
  </div>

</template>

<script>

import setParticles from '@/utils/setPartcles'
import request from "@/utils/request";
import Cookie from "js-cookie";

export default {
  name: "Login",
  data() {
    const checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('君の名前は？'));
      }
      callback();
    };
    const checkPassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error('密码在哪里？'))
      } else {
        callback()
      }
    };

    return {
      admin: {},
      rules: {
        name: [
          {validator: checkName, trigger: 'blur'},
          {min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur'}
        ],
        password: [
          {validator: checkPassword, trigger: 'blur'},
          {min: 4, max: 10, message: '长度在 4 到 10 个字符', trigger: 'blur'}
        ]
      },
    }
  },
  mounted() {
    particlesJS('particles-js', setParticles);
  },
  methods: {
    submitLogin() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          request.post("/admin/login", this.admin).then(res => {
            if (res.code === '200') {
              if (res.data !== null) {
                Cookie.set('admin', JSON.stringify( res.data));
              }
              this.$message.success("登录成功")
              this.$router.push("/")
            } else {
              this.$message.error(res.msg);
            }
          })
        } else {
          return false;
        }

      })
    },
  }
};
</script>

<style>
.login {
  height: 100%;
  width: 100%;
  /*overflow: hidden;*/
  background-size: cover;
  /*background-repeat: no-repeat;*/
  background-image: url('@/assets/tomoyo.jpg');
  /*filter:"progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='scale')";*/
  /*-moz-background-size:100% 100%;*/
  /*background-size:100% 100%;*/
  opacity: 0.9;
  position: fixed;
  /*pointer-events: none;*/
}


.loginPart {
  position: fixed;
  /*定位方式绝对定位absolute*/
  top: 50%;
  left: 20%;
  /*顶和高同时设置50%实现的是同时水平垂直居中效果*/
  transform: translate(-50%, -50%);
  /*实现块元素百分比下居中*/
  width: 300px;
  /*padding: 10px;*/
  background: rgb(197, 179, 229, 0.3);
  /*背景颜色为黑色，透明度为0.8*/
  /*box-sizing: border-box;*/
  /*box-sizing设置盒子模型的解析模式为怪异盒模型，
  将border和padding划归到width范围内*/
  box-shadow: 0px 15px 25px rgba(0, 0, 0, .5);
  /*边框阴影  水平阴影0 垂直阴影15px 模糊25px 颜色黑色透明度0.5*/
  border-radius: 15px;
  /*边框圆角，四个角均为15px*/

}

.formpart {
  opacity: 0.7;

}

.item .el-form-item__label {
  color: wheat;
}

h3 {
  color: #c5b3e5;
  text-align: center;
  /*文字居中*/
  /*color: #00ffff;*/
  /*background: #c5b3e5;*/
}


</style>



